package csu.web.credit_bank.controller;

import csu.web.credit_bank.pojo.dto.ArticleCreateDto;
import csu.web.credit_bank.pojo.Articles;
import csu.web.credit_bank.service.ArticlesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/articles")
@CrossOrigin
public class ArticlesController {

    @Autowired
    private ArticlesService articleService;

    @PostMapping("")
    public ResponseEntity<Articles> createArticle(@RequestBody ArticleCreateDto articleDto) {
        Articles createdArticle = articleService.createArticle(articleDto);
        return ResponseEntity.ok(createdArticle);
    }


    @GetMapping("/{id}")
    public ResponseEntity<List<Articles>> getAllArticles(@PathVariable String id) {
        List<Articles> articles = articleService.getAllArticles(id);
        return ResponseEntity.ok(articles);
    }
    @PutMapping("{articleId}/{userId}")
    public ResponseEntity<Map<String, Object>> updateArticleLikes(@PathVariable String articleId,@PathVariable String userId) {
        articleService.updateArticleLikes(articleId,userId);
        Map<String, Object> responseBody = new HashMap<>();
        responseBody.put("code", 200);
        responseBody.put("message", "点赞状态修改成功");
        responseBody.put("data", articleId);
        return ResponseEntity.ok(responseBody);
    }
    @GetMapping("/hotList")
    public ResponseEntity<List<Articles>> getHotArticles() {
        List<Articles> articlesList = articleService.getHotArticlesInLast24Hours();
        return ResponseEntity.ok(articlesList);
    }
}
